//
//  AddressListVC.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/24.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "AddressListVC.h"
#import "AddressListModel.h"
#import "AddressListCell.h"
#import "EditAddressInfoVC.h"

@interface AddressListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *addressArray;//数据源
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation AddressListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货地址";
    self.view.backgroundColor = [UIColor lightGrayColor];
    //获取json数据
    [self getJsonData];
    //创建列表
    [self.view addSubview:self.myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - 获取数据
-(void)getJsonData{
    NSDictionary *detailDic = [AppTools readLocalFileWithName:@"data"];
    self.addressArray = [AddressListModel mj_objectArrayWithKeyValuesArray:detailDic[@"result"]];
}

#pragma mark - 懒加载
-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.backgroundColor = [UIColor lightGrayColor];
        [_myTableView registerClass:[AddressListCell class] forCellReuseIdentifier:@"AddressListCell"];
        
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kAppWidth_Mini(360))];
        _myTableView.tableFooterView = footerView;
        UIButton *addButton = [UIButton initWithTitleStr:@"+新增收货地址" titleColor:[UIColor blackColor] withFont:UIPFThinFont(15)];
        [addButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
        addButton.layer.borderColor = [UIColor grayColor].CGColor;
        addButton.backgroundColor = [UIColor whiteColor];
        addButton.layer.borderWidth = 1;
        [footerView addSubview:addButton];
        [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kAppWidth_Mini(830));
            make.height.mas_equalTo(kAppWidth_Mini(130));
            make.centerX.centerY.equalTo(footerView);
        }];
        
    }
    return _myTableView;
}

#pragma mark - UITableviewDelegate && UITableviewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.addressArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressListCell"];
    __weak typeof(self) weakSelf = self;
    AddressListModel *model = self.addressArray[indexPath.section];
    cell.cellButtonAction = ^(NSString * _Nonnull buttonTitle) {
        [weakSelf cellActionWith:[NSIndexPath indexPathForRow:0 inSection:[self.addressArray indexOfObject:model]] ButtonTitle:buttonTitle];
    };
    cell.listModel = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kAppWidth_Mini(420);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? kAppWidth_Mini(40) : kAppWidth_Mini(20);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

#pragma mark - cell按钮点击事件
-(void)cellActionWith:(NSIndexPath *)indexPath ButtonTitle:(NSString *)buttonTitle{
    AddressListModel *model = self.addressArray[indexPath.section];
    if ([buttonTitle isEqualToString:@"设为默认"]) {
        [self.addressArray setValue:@(NO) forKeyPath:@"isdefault"];
        model.isdefault = YES;
        [self.myTableView reloadData];
    }else if([buttonTitle isEqualToString:@"删除"]){
        [self.addressArray removeObjectAtIndex:[self.addressArray indexOfObject:model]];
        [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }else if([buttonTitle isEqualToString:@"编辑"]){
        EditAddressInfoVC *vc = [EditAddressInfoVC new];
        vc.addressModel = [model mutableCopy];
        __weak typeof(self) weakSelf = self;
        vc.refreshModel = ^(AddressListModel * _Nonnull model) {
            [weakSelf.addressArray replaceObjectAtIndex:indexPath.section withObject:model];
            [weakSelf.myTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 添加收货地址
-(void)addAction{
    EditAddressInfoVC *vc = [EditAddressInfoVC new];
    __weak typeof(self) weakSelf = self;
    vc.refreshModel = ^(AddressListModel * _Nonnull model) {
        [weakSelf.addressArray addObject:model];
        [weakSelf.myTableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
